package com.pass.util.dialog;

import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;

import com.pass.util.R;


public class DatePickerDialog extends BaseDialogFragment {
    private TextView titleView;
    private DatePicker datePicker;

    private TextView cancelBtn;
    private TextView okBtn;
    private DateCallback callback;
    private String sureStr;
    private String cancelStr;
    private String titleStr;


    public DatePickerDialog(String title, DateCallback dialogCallback) {
        super(DialogPosition.CENTER, true);
        this.sureStr = "确定";
        this.cancelStr = "取消";
        if (!TextUtils.isEmpty(title)) {
            this.titleStr = title;
        }
        this.callback = dialogCallback;
    }

    public void setDateRange(Long minSysInMill, Long maxSysInMill) {
        datePicker.setMinDate(minSysInMill);
        datePicker.setMaxDate(maxSysInMill);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.dlg_datepicker;
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.MyDialogStyle;
    }

    @Override
    protected void initView(View rootView) {
        titleView = rootView.findViewById(R.id.tv_title);
        datePicker = rootView.findViewById(R.id.date_picker);
        cancelBtn = rootView.findViewById(R.id.tv_cancel);
        okBtn = rootView.findViewById(R.id.tv_ok);

        datePicker.setCalendarViewShown(false);
    }

    @Override
    protected void initData() {
        cancelBtn.setText(cancelStr);
        okBtn.setText(sureStr);
        if (!TextUtils.isEmpty(this.titleStr)) {
            titleView.setText(this.titleStr);
        }
    }

    @Override
    protected void initListener() {
        okBtn.setOnClickListener(v -> {
            if (null != callback) {
                String dateStr = String.format("%d-%02d-%02d", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth());
                callback.onOKClicked(dateStr);
            }
            dismiss();
        });
        cancelBtn.setOnClickListener(v -> {
            if (null != callback) {
                callback.onCancelled();
            }
            dismiss();
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(R.color.translucent);
        WindowManager.LayoutParams attributes = window.getAttributes();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        attributes.width = displayMetrics.widthPixels / 12 * 5;
        attributes.height = displayMetrics.heightPixels / 12 * 5;
        window.setAttributes(attributes);

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
